package com.easy.web.controller;

import com.easy.web.model.Slide;
import com.easy.web.service.SlideService;
import com.easy.web.utils.StringUtil;
import com.easy.web.utils.ViewUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/12/29.
 */
@Controller
@RequestMapping("slide")
public class SlideController {

    @Autowired
    private SlideService slideService;

    /**
     * 获取幻灯片列表
     */
    @RequestMapping(value = "getSlideList",method = {RequestMethod.GET})
    public ModelAndView getSlideList(){
        List<Slide> slideList =slideService.getSlideList();

        return new ModelAndView("", ViewUtils.mapSuccess(slideList));
    }


    /**
     * 新增一个幻灯片
     */
    @RequestMapping(value = "addSlide",method = {RequestMethod.POST})
    public ModelAndView addSlide(@RequestParam(value = "setSlideId",required = true) String seq,
                                 @RequestParam(value = "setSlideLink",required = true) String link,
                                 @RequestParam(value = "setSlidePic",required = true) String picUrl){
        Slide slide=new Slide();
        slide.setId(UUID.randomUUID().toString());
        slide.setLink(link);
        slide.setPicUrl(picUrl);
        slide.setCreateTime(new Date());
        if(StringUtil.isNumeric(seq)){
            Slide slideTmp=slideService.querySlideBySeq(seq);
            if(slideTmp==null){
                slide.setSeq(seq);
            }else {
                return new ModelAndView("", ViewUtils.mapFail("输入的顺序已存在，请重新输入"));
            }
        }else {
            return new ModelAndView("", ViewUtils.mapFail("请输入正确的正整数"));
        }

        try {
            slideService.addSlide(slide);
        }catch (Exception e){
            e.printStackTrace();
            return new ModelAndView("", ViewUtils.mapFail("上传失败"));
        }

        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }

    /**
     * 删除一个幻灯片
     */
    @RequestMapping(value = "delSlide",method = {RequestMethod.POST})
    public ModelAndView addSlide(@RequestParam(value = "delSlideUid",required = true) String id){

        slideService.delSlide(id);
        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }


    /**
     * 更新一个幻灯片信息
     */
    @RequestMapping(value = "updateSlide",method = {RequestMethod.POST})
    public ModelAndView updateSlide(@RequestParam(value = "editSlideUid",required = true) String id,
                                    @RequestParam(value = "editSlideId",required = true) String seq,
                                    @RequestParam(value = "editSlideLink",required = true) String link,
                                    @RequestParam(value = "editSlidePic",required = true) String picUrl){

        Slide slide=slideService.querySlideById(id);
        if(slide == null){
            return new ModelAndView("", ViewUtils.mapFail("编辑的该幻灯片不存在，请重新编辑"));
        }else {
            slide.setLink(link);
            slide.setPicUrl(picUrl);
            slide.setCreateTime(new Date());
            if(!slide.getSeq().equals(seq)) {
                if (StringUtil.isNumeric(seq)) {
                    Slide slideT = slideService.querySlideBySeq(seq);
                    if (slideT != null) {
                        return new ModelAndView("", ViewUtils.mapFail("输入的顺序已存在，请重新输入"));
                    } else {
                        slide.setSeq(seq);
                    }
                } else {
                    return new ModelAndView("", ViewUtils.mapFail("请输入正确的正整数"));
                }
            }
        }

        try {
            slideService.updateSlide(slide);
        }catch (Exception e){
            e.printStackTrace();
            return new ModelAndView("", ViewUtils.mapFail("上传失败"));
        }


        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }
}
